/*
 * $Header: $
 * $Id$
 *
 * Copyright (c) 2011, Michael DePhillips. All Rights Reserved.
 * This code may not be used without the express written permission
 * of the copyright holder, Michael DePhillips.
 */

package com.facebook.bannermaker.android.activity.template;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;

import com.facebook.bannermaker.android.R;


/**
 * Activity for how to display the banners in a list view
 *
 * @author Michael DePhillips
 * 
 */
public class BannerObjectListAdaptor extends BaseAdapter {

    //    private static final String LOG_TAG = BrowseObjectListAdaptor.class.getName();
    //    implements OnClickListener
    private List<Bitmap> m_imageViews = null;
    private Context m_context;


    /**
     * 
     * @param browseObjectBeanList
     * @param context
     */
    public BannerObjectListAdaptor(List<Bitmap> imageViews, Context context) {
        m_imageViews = imageViews;
        m_context = context;
    }

    /**
     * Configure each object row.
     */
    //    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Bitmap banner = m_imageViews.get(position);
        LinearLayout itemLayout = (LinearLayout) LayoutInflater.from(m_context).inflate(R.layout.template_banner_item, parent,
                false);

        TableRow tableRow = (TableRow) itemLayout.findViewById(R.id.BrowseObjectRow);
        tableRow.setTag(banner);

        // set image dimensions manually because ImageViews suck
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) m_context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = width * banner.getHeight() / banner.getWidth();
        
        // apply the banner to the imageview
        ImageView imageView = (ImageView) itemLayout.findViewById(R.id.template_banner_image);
        imageView.setLayoutParams(new LinearLayout.LayoutParams( width, height));
        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(banner);

        return itemLayout;

    }

    //    @Override
    public int getCount() {
        return m_imageViews.size();
    }

    //    @Override
    public Bitmap getItem(int position) {
        return m_imageViews.get(position);
    }

    //    @Override
    public long getItemId(int position) {
        return 0;
    }
}
